package servise

import (
	"hxy/repo/database"
	errors "hxy/repo/error"
	. "hxy/repo/model"
	"strconv"

	"github.com/emicklei/go-restful/v3"
)

func HandleCreateAuth(request *restful.Request, response *restful.Response) {
	// Retrieve you data in one line of code!
	gitauth := new(GitAuth)
	if err := request.ReadEntity(gitauth); err != nil {
		errors.HandleInternalError(response, err)
		return
	}
	err := database.AuthCreate(*gitauth)
	if err != nil {
		errors.HandleInternalError(response, err)
		return
	}
	response.WriteAsJson(gitauth)

}
func HandlerGetAllGitAuth(request *restful.Request, response *restful.Response) {
	// Retrieve you data in one line of code!
	all, dbresult := database.AuthGetAll()
	if dbresult.Error != nil {
		errors.HandleInternalError(response, dbresult.Error)
		return
	}
	response.WriteAsJson(all)

}
func HandlerDeleteGitAuth(request *restful.Request, response *restful.Response) {
	var id = request.PathParameter("id")
	aid, err := strconv.Atoi(id)
	if err != nil {
		errors.HandleInternalError(response, err)
	}
	re := database.AnthDeleteId(aid)
	if re.Error != nil {
		errors.HandleInternalError(response, re.Error)
	}
	if re.RowsAffected == 0 {
		response.WriteAsJson(false)
	} else {
		response.WriteAsJson(true)
	}

}
